/**
 * Copyright (C) 2011 KingCore Science & Technology CO,.LTD. All rights reserved.
 * XNS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/**
 * Copyright (C) 2002-2011 WUZEWEN. All rights reserved.
 * WUZEWEN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 * This software is the confidential and proprietary information of
 * WuZeven, Personal. ("Confidential Information").  You shall not
 * disclose such Confidential Information and shall use it only in
 * accordance with the terms of the license agreement you entered into
 * with WuZeven.
 */

package com.kingcore.director.main;

import java.awt.CheckboxMenuItem;
import java.awt.HeadlessException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Observable;
import java.util.Observer;
import java.util.ResourceBundle;

import javax.swing.JFrame;

import wzw.util.I18nUtil;
import wzw.util.I18nUtils;

import com.kingcore.director.DirectorConstants;
import com.kingcore.director.core.util.DirectorComponentUtils;
import com.kingcore.director.resourcemaster.ui.ResourceMasterMainPanel;

/**
 * <p>java类文件的说明...</p>
 * @author Zeven/wzw on Jul 16, 2011
 * @version	1.0
 * @see		Object#equals(java.lang.Object)
 * @since	JDK5
 */

public class TrayPopupMenu extends PopupMenu implements ActionListener, Observer, ItemListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

    MenuItem  tray_Menu_Exit  = null; 
    MenuItem  tray_Menu_Reminder  = null; 
    MenuItem  tray_Menu_Calendar = null; 
    MenuItem  tray_Menu_LocalSearch  = null; 
    MenuItem  tray_Menu_FileExploer  = null; 
    MenuItem  tray_Menu_FileCopy  = null; 
    MenuItem  tray_Menu_FavoriteFiles  = null; 
    MenuItem  tray_Menu_DocumentLibrary  = null; 
    MenuItem  tray_Menu_ProgramLibrary  = null; 
    MenuItem  tray_Menu_OpenMainWindow  = null; 
    MenuItem  tray_Menu_OpenMiniWindow  = null; 
    CheckboxMenuItem  tray_Menu_OpenNoDisturb = null; 
    
    public MainFrame mainFrame = null; 
	
	public TrayPopupMenu(MainFrame mainFrame) throws HeadlessException {
		super();
		this.mainFrame = mainFrame;
		initializeInterface();
		this.update(null, null);
	}

	private void initializeInterface(){
        tray_Menu_Exit  = new MenuItem("Exit"); 
        //tray_Menu_Exit.setAccelerator( KeyStroke.getKeyStroke(KeyEvent.VK_T,Event.CTRL_MASK) );
        //DirectorMenuBar dmb = this.getReminderMainPanel().getMenuBar();
        tray_Menu_Reminder  = new MenuItem("My Reminder"); 
        tray_Menu_Reminder.addActionListener(this);      
        tray_Menu_Calendar = new MenuItem("My Calendar"); 
        tray_Menu_Calendar.addActionListener(this);      
        tray_Menu_LocalSearch  = new MenuItem("Local Search"); 
        tray_Menu_LocalSearch.addActionListener(this);      
        tray_Menu_FileExploer  = new MenuItem("File Exploer"); 
        tray_Menu_FileExploer.addActionListener(this);      
        tray_Menu_FileCopy = new MenuItem("File Copy"); 
        tray_Menu_FileCopy.addActionListener(this);      
        tray_Menu_FavoriteFiles  = new MenuItem("Favorite Files"); 
        tray_Menu_FavoriteFiles.addActionListener(this);  
        tray_Menu_DocumentLibrary  = new MenuItem("Document Library"); 
        tray_Menu_DocumentLibrary.addActionListener(this);      
        tray_Menu_ProgramLibrary  = new MenuItem("Program Library"); 
        tray_Menu_ProgramLibrary.addActionListener(this);  
        tray_Menu_OpenMainWindow  = new MenuItem("Open Main Window"); 
        tray_Menu_OpenMainWindow.addActionListener(this);
        tray_Menu_OpenMiniWindow  = new MenuItem("Show Mini Window"); 
        tray_Menu_OpenMiniWindow.addActionListener(this);    
        tray_Menu_OpenNoDisturb  = new CheckboxMenuItem("Open No Disturb"); 
        tray_Menu_OpenNoDisturb.setState(Main.isNoDisturbOpen);
        tray_Menu_OpenNoDisturb.addItemListener(this);
        //tray_Menu_NoDisturb.addActionListener(this);      
        tray_Menu_Exit.addActionListener(this);    
        
        this.add(tray_Menu_Reminder); 
        this.add(tray_Menu_Calendar); 
        this.add(tray_Menu_LocalSearch); 
        this.addSeparator(); 
        this.add(tray_Menu_FileExploer);
        this.add(tray_Menu_FileCopy);
        this.add(tray_Menu_FavoriteFiles); 
        this.addSeparator(); 
        this.add(tray_Menu_DocumentLibrary); 
        this.add(tray_Menu_ProgramLibrary); 
        this.addSeparator(); 
        this.add(tray_Menu_OpenMainWindow);
        this.add(tray_Menu_OpenMiniWindow);
        this.add(tray_Menu_OpenNoDisturb);
        this.addSeparator(); 
        this.add(tray_Menu_Exit);  
	}
	
	public TrayPopupMenu(String label) throws HeadlessException {
		super(label);
		// TODO Auto-generated constructor stub
	}
	/**
	 * <p>java方法的说明...</p>
	 * @author Zeven on Jul 16, 2011
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}
	
	public void actionPerformed(ActionEvent e) {
		Object eventObj = e.getSource();
		if (eventObj == tray_Menu_Exit) {  //退出
            try{
                System.exit(0);     
                return ;
            }catch(Exception   ex)   {   
                 ex.printStackTrace();   
             }   
		
		} else if (eventObj == tray_Menu_OpenMiniWindow) { //现实迷你窗口
			MiniDialog miniDialog = MiniDialog.getInstance();
			miniDialog.setVisible(true);  
			miniDialog.getInputTextField().requestFocusInWindow(); //不分开写第一次从tray打开不能获取焦点。
            return ;
		}

		if (eventObj == tray_Menu_Reminder) {
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_ReminderMainPanel_Index );	
			
		} else if (eventObj == tray_Menu_Calendar) {
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_CalendarMainPanel_Index );	
			
		} else if (eventObj == tray_Menu_LocalSearch) {
			this.mainFrame.getMainTabbedPane().setSelectedIndex( MainFrame.MainTabPane_LocalSearchMainPanel_Index );	
			
		} else if (eventObj == tray_Menu_FileExploer) {
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileExplorer_Index );

		} else if (eventObj == tray_Menu_FileCopy) {
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FileCopy_Index );

		} else if (eventObj == tray_Menu_FavoriteFiles) {
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_FavoriteFiles_Index );
	
		} else if (eventObj == tray_Menu_ProgramLibrary) {
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_ProgramLibrary_Index );
	
		} else if (eventObj == tray_Menu_DocumentLibrary) {
			DirectorComponentUtils.openTabpage( mainFrame.getMainTabbedPane(), MainFrame.MainTabPane_ResourceMasterMainPanel_Index );
			DirectorComponentUtils.openTabpage( mainFrame.getResourceMasterMainPanel().getMainTabbedPane(), ResourceMasterMainPanel.MainTabPane_DocumentLibrary_Index );
	
		} else if (eventObj == tray_Menu_OpenMainWindow) {
//			this.mainFrame.setExtendedState(NORMAL);
//			this.mainFrame.setVisible(true);
		}

    	//if (this.mainFrame.getExtendedState()==JFrame.ICONIFIED) {
		// setVisible要放在setExendedState前面。
		this.mainFrame.setVisible(true);
        this.mainFrame.setExtendedState( JFrame.NORMAL);
		//}
		
	}

	@Override
	public void update(Observable o, Object arg) {
		//System.out.println(Locale.getDefault());
		ResourceBundle bundle = I18nUtils.getBundle(DirectorConstants.I18n_Language_Name);

		// popup menu for table 
		tray_Menu_OpenMainWindow.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_OpenMainWindow.Text"));
		tray_Menu_OpenMiniWindow.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_OpenMiniWindow.Text"));
		tray_Menu_DocumentLibrary.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_DocumentLibrary.Text"));
        tray_Menu_ProgramLibrary.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_ProgramLibrary.Text"));
        tray_Menu_FavoriteFiles.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_FavoriteFiles.Text"));
        tray_Menu_FileCopy.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_FileCopy.Text"));
        tray_Menu_FileExploer.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_FileExploer.Text"));
        tray_Menu_LocalSearch.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_LocalSearch.Text"));
        tray_Menu_Calendar.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_Calendar.Text"));
        tray_Menu_Reminder.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_Reminder.Text"));
        tray_Menu_Exit.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_Exit.Text"));
        tray_Menu_OpenNoDisturb.setLabel(I18nUtil.getBundleString(bundle, TrayPopupMenu.class, "tray_Menu_OpenNoDisturb.Text"));
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		Object eventObj = e.getSource();
		if (eventObj == tray_Menu_OpenNoDisturb){
			mainFrame.setOpenNoDisturbState(Main.isNoDisturbOpen);
	        return; //这里不需要不打开窗口
		}
	}

}
